<?php

/* 打招呼 */
namespace app\admin\controller;

use app\admin\logic\SayhiLogic;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class SayhiController extends AdminBaseController
{

    public function index()
    {
        $params = $this->request->get();
        $countryData = $this->recommendLanguageData;
        if (empty($params['langId'])) {
            $params['langId'] = $countryData[0]['id'];
            $this->assign('langId', $this->defaultLanguage['id']);
        }else {
            $this->assign('langId', $params['langId']);
        }
        $list = SayhiLogic::getData($params);
        
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign('list', $list);
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }


    public function add()
    {
        $this->assign('langData', $this->recommendLanguageData);
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->post();
            $name=$data['name'];
            
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $condition = [
                    'del_flag' => 0,
                    'name' => $name,
                    'lang_id' => $data['lang_id']
                ];
                $check = Db::name('sayhi')->where($condition)->find();
                if($check){
                    $this->error('名称已存在');
                }
            }

            $id = DB::name('sayhi')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('sayhi')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $this->assign('data', $data);
        $this->assign('countryData', $this->recommendLanguageData);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            
            $id=$data['id'];
            $name=$data['name'];
            if($name == ''){
                $this->error('请填写名称');
            }else{
                $condition = [
                    'del_flag' => 0,
                    'name' => $name
                ];
                $check = Db::name('sayhi')->where($condition)->where(" id !='{$id}'")->find();
                if($check){
                    $this->error('名称已存在');
                }
            }
            

            $rs = DB::name('sayhi')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }
    
    public function listOrder()
    {
        $model = DB::name('sayhi');
        parent::listOrders($model);
        $this->resetcache();
        $this->success("排序更新成功！");
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = DB::name('sayhi')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("sayhi/index"));
    }


    protected function resetcache(){

        $key='sayhi';

        $level=DB::name('sayhi')
                ->field('id,name, lang_id')
                ->order("sort asc")
                ->select();
        if($level){
            setcaches($key,$level);
        }
    }
}